package servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Customer;
import bean.Orders;
import bean.Room;
import common.base.BaseServlet;
import service.OrderService;
import service.RoomService;
import service.impl.OrderServiceImpl;
import service.impl.RoomServiceImpl;



@WebServlet("/OrderServlet")
public class OrderServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	public String ui(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		return "/user/order.jsp";
	}
		
	
	public String addOrder (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		
		//接受请求商品信息
		String roomId = request.getParameter("roomId");
		int pcount = Integer.valueOf(request.getParameter("pcount"));
		//根据商品id查询出房间
		RoomService roomService = new RoomServiceImpl();
		Room room = roomService.queryRoomById(roomId);
		
		Customer u = (Customer) request.getSession().getAttribute("user");
		
		//预订业务逻辑
		OrderService orderService = new OrderServiceImpl();
		orderService.addOrder(u,room, pcount);
		
		//重定向到ui画面
		response.sendRedirect(request.getContextPath() + "/OrderServlet?action=ui");
		
		return null;
	}
	
	//移除购物车
//	public String removeCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		
//		//接受删除的商品id
//		String pid = request.getParameter("pid");
//		
//		//取出购物车
//		Car car = (Car) request.getSession().getAttribute("car");
//		
//		//购物车业务逻辑
//		CarService carService = new CarServiceImpl();
//		carService.removeCar(pid, car);
//		
//		//需要将car重新放入session
//		request.getSession().setAttribute("car", car);
//		
//		//重定向到ui画面
//		response.sendRedirect(request.getContextPath() + "/CarServlet?action=ui");
//		
//		return null;
//	}

}
